(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.glSystem.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'modalService',
			'glSystemService' ];
	function deleteController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, modalService, glSystemService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.glSystem = {};
		glSystemService.initPageCallback(ctrl);
		ctrl.init = function() {
			glSystemService.getGlSystemLayout('delete').then(function(layoutRsponse) {
				if (!layoutRsponse.data) {
					return;
				}
				ctrl.layoutConfig = layoutRsponse.data;
				glSystemService.loadOptions().then(function(glSystemOptions) {
					var glSystem;
					glSystemService.initOptions(ctrl, glSystemOptions);
					authService.delete(params).then(function(response) {
						glSystem = response.data;
						if (glSystem) {
							ctrl.feeamortmethodOption = glSystem.feeamortMethodList;
							ctrl.pastduemethodOption = glSystem.pastdueMethodList;
							ctrl.glSystem = glSystem;
				    	}
					});
				});
			});
		};
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.glSystem.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.glSystem).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};

		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.glSystem.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.glSystem.list',
					'state' : 'cda.glSystem.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
